package cn.wolfcode.web.controller;

import cn.wolfcode.domain.Employee;
import cn.wolfcode.qo.EmployeeQueryObject;

import cn.wolfcode.qo.JsonResult;
import cn.wolfcode.service.IDepartmentService;
import cn.wolfcode.service.IEmployeeService;
import cn.wolfcode.service.IRoleService;
import cn.wolfcode.util.UserContext;
import com.alibaba.fastjson.JSON;
import com.sun.org.apache.bcel.internal.generic.NEW;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.HashMap;

@Controller
@RequestMapping("/employee")
public class EmployeeController {

    @Autowired
    private IEmployeeService employeeService;

    @Autowired
    private IDepartmentService departmentService;

    @Autowired
    private IRoleService roleService;

    // 处理新增请求 http://localhost/employee/saveOrUpdate.do POST  name: adf sn: asdf
    @RequestMapping("/saveOrUpdate")
//    @RequiredPermission(name = "员工新增/编辑",expression = "employee:list")
    @ResponseBody
    public JsonResult saveOrUpdate(Employee employee,Long[] ids) {
        if (employee.getId() != null) { // 修改
            employeeService.update(employee,ids);
        }else { // 新增
            employeeService.save(employee,ids);
        }
        return new JsonResult();
    }

    // 处理去新增请求 http://localhost/employee/input.do
    @RequestMapping("/input")
    public String input(Long id, Model model){
        model.addAttribute("roles",roleService.listALL());

        model.addAttribute("departments", departmentService.listALL());

        // 若传 id 值，表示是去修改，那就要去查询被修改的员工对象，存在模型中
        if (id != null) {
            model.addAttribute("employee", employeeService.get(id));
        }
        return "employee/input"; // /WEB-INF/views/employee/input.ftl
    }

    
    // 处理删除员工请求 http://localhost/employee/delete.do?id=1
    @RequestMapping("/delete")
//    @RequiredPermission(name = "员工删除",expression = "")
    public String delete(Long id) {
        if (id != null) { // 传id值就删除对应的员工数据
            employeeService.delete(id);
        }
        return "redirect:/employee/list.do";
    }


    // 处理分页查询员工的请求
    @RequestMapping("/list")
    public String list(Model model, @ModelAttribute("qo") EmployeeQueryObject qo) { // model.addAttribute("qo", qo);
        Employee employee = UserContext.getCurrentUser();


        // 分页查询员工数据，存在模型
        // 怎么把员工对应部门放到模型中呢？
        model.addAttribute("pageInfo", employeeService.query(qo));

        // 查询所有部门数据，存在模型中
        model.addAttribute("departments", departmentService.listALL());

        // 找视图
        return "employee/list"; // /WEB-INF/views/employee/potentialList.ftl
    }

    @RequestMapping("/batchDelete")
    @ResponseBody
    public JsonResult batchDelete(/*@RequestParam("ids[]")*/ Long[] ids){
        employeeService.batchDelete(ids);
        return new JsonResult();
    }


    /**
     * 验证用户名是否存在
     * valid 的key是插件规定的，不能改
     * true 验证通过 false验证不通过
     * @param name
     * @return
     */
    @RequestMapping("/checkName")
    @ResponseBody
    public HashMap checkName(String name,Long id){
        Employee employee = employeeService.selectByName(name);
        HashMap map = new HashMap();
        map.put("valid",employee==null);
        return map;
    }

    /**
     * 导出
     * @param response
     * @throws IOException
     */
    @RequestMapping("/exportXls")
    public void exportXls(HttpServletResponse response) throws IOException {
        //设置文件下载的响应头
        response.setHeader("Content-Disposition","attachment;filename=employee.xls");
        //在业务层中创建excel
        Workbook wb = employeeService.exportXls();
        //把excel的数据输出到浏览器的响应流中
        wb.write(response.getOutputStream());
    }

    /**
     * 导入
     */
    @RequestMapping("/importXls")
    @ResponseBody
    public JsonResult importXls(MultipartFile file) throws IOException {
        employeeService.importXls(file);
        return new JsonResult();
    }
}
